package com.yhb.beans;

import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

@Log4j
public class ConsumerBean extends com.aliyun.openservices.ons.api.bean.ConsumerBean {

    private static volatile boolean initialized = false;
    private final byte[] LOCK = new byte[0];

    @Value("${CLUSTER_MODE}")
    private String clusterMode;

    @Override
    public void start() {
        log.warn("ENV:MODE=" + clusterMode);
        if ("CONSUMER".equals(clusterMode)) {
            synchronized (LOCK) {
                if (!initialized) {
                    super.start();
                    log.info("consumer started");
                    initialized = true;
                }
            }
        }

    }

    @Override
    public void shutdown() {
        synchronized (LOCK) {
            if (initialized) {
                super.shutdown();
                log.info("consumer started");
                initialized = false;
            }
        }
    }
}
